local sk__dingming = fk.CreateSkill {

  name = "sk__dingming",

  tags = {  },

}



sk__dingming:addEffect(fk.TurnStart, {
  name = "sk__dingming",
  anim_type = "masochism",
  events = {fk.TurnStart, fk.Damaged, fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if target and target == player and player:hasSkill(sk__dingming.name) then
      if event == fk.TurnStart then
        return true
      elseif event == fk.Damaged then
        return data.from and data.from ~= player
      elseif event == fk.Damage then
        return data.to ~= player and not data.to.dead
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnStart or event == fk.Damaged then
      event:setCostData(self, player)
    else
      event:setCostData(self, data.to)
    end
    return player.room:askForSkillInvoke(player, sk__dingming.name, nil, "#sk__dingming-invoke::"..event:getCostData(self).id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self)
    room:doIndicate(player.id, {to.id})
    local n = math.max(0, to.hp) - math.min(to.maxHp, to:getLostHp())
    if n > 0 then
      room:loseHp(to, n, sk__dingming.name)
    elseif n < 0 and to:isWounded() then
      room:recover({
        who = to,
        num = -n,
        recoverBy = player,
        skillName = sk__dingming.name,
      })
    end
    if to == player and not player.dead and player.hp > player:getLostHp() then
      room:changeMaxHp(player, -1)
    elseif to ~= player and not player.dead and to.hp < to:getLostHp() then
      room:changeMaxHp(player, -1)
    end
    if not player.dead and n ~= 0 then
      player:drawCards(math.abs(n), sk__dingming.name)
    end
  end,
})
sk__dingming:addEffect(fk.Damaged, {
  name = "sk__dingming",
  anim_type = "masochism",
  events = {fk.TurnStart, fk.Damaged, fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if target and target == player and player:hasSkill(sk__dingming.name) then
      if event == fk.TurnStart then
        return true
      elseif event == fk.Damaged then
        return data.from and data.from ~= player
      elseif event == fk.Damage then
        return data.to ~= player and not data.to.dead
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnStart or event == fk.Damaged then
      event:setCostData(self, player)
    else
      event:setCostData(self, data.to)
    end
    return player.room:askForSkillInvoke(player, sk__dingming.name, nil, "#sk__dingming-invoke::"..event:getCostData(self).id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self)
    room:doIndicate(player.id, {to.id})
    local n = math.max(0, to.hp) - math.min(to.maxHp, to:getLostHp())
    if n > 0 then
      room:loseHp(to, n, sk__dingming.name)
    elseif n < 0 and to:isWounded() then
      room:recover({
        who = to,
        num = -n,
        recoverBy = player,
        skillName = sk__dingming.name,
      })
    end
    if to == player and not player.dead and player.hp > player:getLostHp() then
      room:changeMaxHp(player, -1)
    elseif to ~= player and not player.dead and to.hp < to:getLostHp() then
      room:changeMaxHp(player, -1)
    end
    if not player.dead and n ~= 0 then
      player:drawCards(math.abs(n), sk__dingming.name)
    end
  end,
})
sk__dingming:addEffect(fk.Damage, {
  name = "sk__dingming",
  anim_type = "masochism",
  events = {fk.TurnStart, fk.Damaged, fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if target and target == player and player:hasSkill(sk__dingming.name) then
      if event == fk.TurnStart then
        return true
      elseif event == fk.Damaged then
        return data.from and data.from ~= player
      elseif event == fk.Damage then
        return data.to ~= player and not data.to.dead
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnStart or event == fk.Damaged then
      event:setCostData(self, player)
    else
      event:setCostData(self, data.to)
    end
    return player.room:askForSkillInvoke(player, sk__dingming.name, nil, "#sk__dingming-invoke::"..event:getCostData(self).id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self)
    room:doIndicate(player.id, {to.id})
    local n = math.max(0, to.hp) - math.min(to.maxHp, to:getLostHp())
    if n > 0 then
      room:loseHp(to, n, sk__dingming.name)
    elseif n < 0 and to:isWounded() then
      room:recover({
        who = to,
        num = -n,
        recoverBy = player,
        skillName = sk__dingming.name,
      })
    end
    if to == player and not player.dead and player.hp > player:getLostHp() then
      room:changeMaxHp(player, -1)
    elseif to ~= player and not player.dead and to.hp < to:getLostHp() then
      room:changeMaxHp(player, -1)
    end
    if not player.dead and n ~= 0 then
      player:drawCards(math.abs(n), sk__dingming.name)
    end
  end,
})

return sk__dingming